from google.appengine.ext import db
import models
from datetime import datetime, timedelta

ALL = 1000

delDate = datetime.now() - timedelta(days = 40)
q1 = db.GqlQuery("SELECT * FROM NewPage WHERE pubDate < :1", delDate)
results1 = q1.fetch(ALL)
for result in results1:
    q2 = db.GqlQuery("SELECT * FROM Copyvio WHERE pgtitle = :1", result.title)
    results2 = q2.fetch(ALL)
    db.delete(results2)
    result.delete()